import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter/material.dart';

class FlutterKefuPlugin {
  static const MethodChannel _channel =
      const MethodChannel('flutter_kefu_plugin');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  ///初始化环信客服云
  ///appKey 必填项，appkey获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“AppKey”
  ///tenantId 必填项，tenantId获取地址：kefu.easemob.com，“管理员模式 > 设置 > 企业信息”页面的“租户ID”
  ///logEnable 设置为true后，将打印日志到logcat, 发布APP时应关闭该选项
  static Future<bool> init({
    @required String appKey,
    @required String tenantId,
    bool logEnable,
  }) async {
    assert(appKey != null && tenantId != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['appKey'] = appKey;
    args['tenantId'] = tenantId;
    if (logEnable != null) {
      args['logEnable'] = logEnable;
    }
    bool result = await _channel.invokeMethod('init', args);
    return result;
  }

  ///获取登录状态
  ///先判断登录状态再进入会话界面，否则会出问题
  static Future<bool> get isLoggedIn async {
    bool isLoggedIn = await _channel.invokeMethod('getLoginStatus');
    return isLoggedIn;
  }

  ///登录
  ///username 账号
  ///password 密码
  static Future<bool> login({
    @required String username,
    @required String password,
  }) async {
    assert(username != null && password != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['username'] = username;
    args['password'] = password;
    bool result = await _channel.invokeMethod('login', args);
    return result;
  }

  ///退出登录
  ///unbindFlag 是否解绑推送的devicetoken
  static Future<bool> logout({
    bool unbindFlag,
  }) async {
    Map<String, dynamic> args = new Map<String, dynamic>();
    if (unbindFlag != null) {
      args['unbindDeviceToken'] = unbindFlag;
    }
    bool result = await _channel.invokeMethod('logout', args);
    return result;
  }

  ///发送文本消息
  ///content 消息文字内容
  ///toChatUsername 为客服设置的IM服务号
  static Future<bool> sendTextMessage({
    @required String content,
    @required String toChatUsername,
  }) async {
    assert(content != null && toChatUsername != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['content'] = content;
    args['toChatUsername'] = toChatUsername;
    bool flag = await _channel.invokeMethod('sendTextMessage', args);
    return flag;
  }

  ///获取未读消息数量
  static Future<int> getUnreadMessagesCount({
    @required String toChatUsername,
  }) async {
    assert(toChatUsername != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['toChatUsername'] = toChatUsername;
    int unreadMessagesCount =
        await _channel.invokeMethod('getUnreadMessagesCount', args);
    return unreadMessagesCount;
  }

  ///所有会话未读消息数清零
  static Future<bool> maskAllConversationsAsRead() async {
    bool result = await _channel.invokeMethod('maskAllConversationsAsRead');
    return result;
  }

  ///获取聊天记录
  static Future<String> getConversationMessages({
    @required String toChatUsername,
  }) async {
    assert(toChatUsername != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['toChatUsername'] = toChatUsername;
    String message =
        await _channel.invokeMethod('getConversationMessages', args);
    return message;
  }

  ///添加消息监听，用来接收消息
  static Future<String> addMessageListener() async {
    String message = await _channel.invokeMethod('addMessageListener');
    return message;
  }

  ///获取企业欢迎语
  static Future<String> getEnterpriseWelcome() async {
    String enterpriseWelcome =
        await _channel.invokeMethod('getEnterpriseWelcome');
    return enterpriseWelcome;
  }

  ///添加排队人数监听
  static Future<int> addVisitorWaitListener() async {
    int num = await _channel.invokeMethod('addVisitorWaitListener');
    return num;
  }

  ///发送图片信息
  static Future<bool> sendImageMessage({
    @required String filePath,
    @required String toChatUsername,
    @required bool sendOriginalImage,
  }) async {
    assert(filePath != null &&
        toChatUsername != null &&
        sendOriginalImage != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['filePath'] = filePath;
    args['toChatUsername'] = toChatUsername;
    args['sendOriginalImage'] = sendOriginalImage;
    bool result = await _channel.invokeMethod('sendImageMessage', args);
    return result;
  }

  ///发送地理位置
  static Future<bool> sendLocationMessage({
    @required double latitude,
    @required double longitude,
    @required String locAddress,
    @required String toChatUsername,
  }) async {
    assert(latitude != null &&
        longitude != null &&
        locAddress != null &&
        toChatUsername != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['latitude'] = latitude;
    args['longitude'] = longitude;
    args['locAddress'] = locAddress;
    args['toChatUsername'] = toChatUsername;
    bool result = await _channel.invokeMethod('sendLocationMessage', args);
    return result;
  }

  static Future<bool> sendFileMessage({
    @required String filePath,
    @required String toChatUsername,
    @required bool sendOriginalImage,
  }) async {
    assert(filePath != null &&
        toChatUsername != null &&
        sendOriginalImage != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['filePath'] = filePath;
    args['toChatUsername'] = toChatUsername;
    bool result = await _channel.invokeMethod('sendFileMessage', args);
    return result;
  }

  ///获取消息总数
  static Future<int> getAllMsgCount({
    @required String toChatUsername,
  }) async {
    assert(toChatUsername != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['toChatUsername'] = toChatUsername;
    int allMsgCount = await _channel.invokeMethod('getAllMsgCount', args);
    return allMsgCount;
  }

  ///删除会话
  static Future<bool> deleteConversation({
    @required String username,
    @required bool deleteMessages,
  }) async {
    assert(username != null && deleteMessages != null);
    Map<String, dynamic> args = new Map<String, dynamic>();
    args['username'] = username;
    args['deleteMessages'] = deleteMessages;
    bool result = await _channel.invokeMethod('deleteConversation', args);
    return result;
  }

  ///添加网络监听，可以显示当前是否连接服务器
  static Future<bool> addConnectionListener() async {
    bool result = await _channel.invokeMethod('addConnectionListener');
    return result;
  }

  ///不需要的时候移除listener，如在activity的onDestroy()时
  static Future<bool> removeMessageListener() async {
    bool result = await _channel.invokeMethod('removeMessageListener');
    return result;
  }

  static Future<bool> removeVisitorWaitListener() async {
    bool result = await _channel.invokeMethod('removeVisitorWaitListener');
    return result;
  }
}
